<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>编辑公告</span>
      </div>
    </template>
    <el-form ref="formRef" :model="form" status-icon label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="是否置顶">
        <el-radio-group v-model="form.is_sticky" class="ml-4">
          <el-radio :label="1" size="large">确定</el-radio>
          <el-radio :label="0" size="large">取消</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="内容" prop="cotent">
        <div style="border: 1px solid #ccc">
          <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig"
            :mode="mode" />
          <Editor style="height: 500px; overflow-y: hidden;" v-model="form.content" :defaultConfig="editorConfig"
            :mode="mode" @onCreated="handleCreated" />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleSubmit(formRef)" type="primary">保存修改</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script setup>
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import notice from '@/api/announce';
import { onBeforeUnmount, ref, inject,shallowRef, onMounted, reactive } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { useRoute, useRouter } from 'vue-router';
// 编辑器实例，必须用 shallowRef
const editorRef = shallowRef()
let mode = ref('simple');
// 内容 HTML
const valueHtml = ref('')
//配置
const toolbarConfig = {}
const editorConfig = { placeholder: '请输入内容...', MENU_CONF: {} }
//上传图片配置
editorConfig.MENU_CONF['uploadImage'] = {
  // form-data fieldName ，默认值 'wangeditor-uploaded-image'
  fieldName: 'file',
  server: 'http://localhost:3001/upload/editor/',
  //自定义http header
  headers: {
    Authorization: `Bearer ${sessionStorage.token}`,
  }
}
// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value
  if (editor == null) return
  editor.destroy()
})
const handleCreated = (editor) => {
  editorRef.value = editor // 记录 editor 实例，重要！
}

let form = ref({
  title: '',
  is_sticky: '0',
  content: ''
})
//获取路由
let router = useRouter();
//获取当前路径
let route = useRoute();
let loadList = async () => {
  let { id } = route.params;
  let { status, data } = await notice.detail({ id });
  if (status) {
    form.value = data;

  }
}
loadList();

// 校验
let formRef = ref();
const rules = reactive({
  title: [],
  is_sticky: [],
  content: [],
})
function handleSubmit(formEl) {
  // console.log(formEl);
  formEl.validate(async (valid, fields) => {
    if (valid) {
      let { status, msg } = await notice.edit({...form.value});
      if (status) {
        ElMessage.success(msg)
        router.push('/Announcement/List');
      } else {
        ElMessage.error(msg);
      }
    } else {
      //没通过校验
      console.log('校验失败字段', fields);
    }
  })
}

</script>
<style scoped>
.editor-box {
  border: 1px solid #ccc;
  border-radius: 3px;
}
</style>
  